package com.yanqu.road.dao.impl.activity.dunhuangshiku;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.dunhuangshiku.DunHuangShiKuMuralInfo;
import com.yanqu.road.entity.activity.dunhuangshiku.DunHuangShiKuTeamData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description
 * @Author cwq
 * @Data 2021/4/19 15:22
 */
public class DunHuangShiKuTeamDataDaoImpl extends TempDataObjectDao<DunHuangShiKuTeamData> {
    public static String getDunHuangShiKuTeamDataSql = "select * from t_c_dunhuangshiku_team_data where activityId=? " +
            "and groupId=? and isDelete = 0";
    public Map<Integer,DunHuangShiKuTeamData> getDunHuangShiKuTeamData(int activityId, int groupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer,DunHuangShiKuTeamData> map = new HashMap();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getDunHuangShiKuTeamDataSql);
            int i = 1;
            call.setLong(i++,activityId);
            call.setInt(i, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                DunHuangShiKuTeamData dunHuangShiKuTeamData = parse(rs);
                map.put(dunHuangShiKuTeamData.getTeamId(),dunHuangShiKuTeamData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateDunHuangShiKuTeamDataSql = "update t_c_dunhuangshiku_team_data set userId=?," +
            "userServerId=?,name=?,createTime=?,updateScoreTime=?,isDelete=?,curFinishMuralId=?,muralInfoMap=? where " +
            "activityId=? and teamId=? and groupId=?";
    @Override
    protected boolean updateData(DunHuangShiKuTeamData dunHuangShiKuTeamData) {
        dunHuangShiKuTeamData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, dunHuangShiKuTeamData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, dunHuangShiKuTeamData.getUserServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, dunHuangShiKuTeamData.getName()));
        para.put(i++, new DbParameter(Types.BIGINT, dunHuangShiKuTeamData.getCreateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, dunHuangShiKuTeamData.getUpdateScoreTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, dunHuangShiKuTeamData.isDelete()));
        para.put(i++, new DbParameter(Types.INTEGER, dunHuangShiKuTeamData.getCurFinishMuralId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(dunHuangShiKuTeamData.getMuralInfoMap())));
        para.put(i++, new DbParameter(Types.BIGINT, dunHuangShiKuTeamData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, dunHuangShiKuTeamData.getTeamId()));
        para.put(i, new DbParameter(Types.INTEGER, dunHuangShiKuTeamData.getGroupId()));
        if (getDb().executeNoneQuery(updateDunHuangShiKuTeamDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertDunHuangShiKuTeamDataSql = "insert into t_c_dunhuangshiku_team_data(activityId,teamId," +
            "userId,userServerId,name,createTime,updateScoreTime,isDelete,curFinishMuralId,muralInfoMap,groupId) " +
            "values (?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(DunHuangShiKuTeamData dunHuangShiKuTeamData) {
        dunHuangShiKuTeamData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, dunHuangShiKuTeamData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, dunHuangShiKuTeamData.getTeamId()));
        para.put(i++, new DbParameter(Types.BIGINT, dunHuangShiKuTeamData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, dunHuangShiKuTeamData.getUserServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, dunHuangShiKuTeamData.getName()));
        para.put(i++, new DbParameter(Types.BIGINT, dunHuangShiKuTeamData.getCreateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, dunHuangShiKuTeamData.getUpdateScoreTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, dunHuangShiKuTeamData.isDelete()));
        para.put(i++, new DbParameter(Types.INTEGER, dunHuangShiKuTeamData.getCurFinishMuralId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(dunHuangShiKuTeamData.getMuralInfoMap())));
        para.put(i, new DbParameter(Types.INTEGER, dunHuangShiKuTeamData.getGroupId()));
        if (getDb().executeNoneQuery(insertDunHuangShiKuTeamDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected DunHuangShiKuTeamData parseData(ResultSet rs) throws SQLException {DunHuangShiKuTeamData dunHuangShiKuTeamData = new DunHuangShiKuTeamData();
        dunHuangShiKuTeamData.setActivityId(rs.getLong("activityId"));
        dunHuangShiKuTeamData.setGroupIdId(rs.getInt("groupId"));
        dunHuangShiKuTeamData.setTeamId(rs.getInt("teamId"));
        dunHuangShiKuTeamData.setUserId(rs.getLong("userId"));
        dunHuangShiKuTeamData.setUserServerId(rs.getLong("userServerId"));
        dunHuangShiKuTeamData.setName(rs.getString("name"));
        dunHuangShiKuTeamData.setCreateTime(rs.getLong("createTime"));
        dunHuangShiKuTeamData.setUpdateScoreTime(rs.getLong("updateScoreTime"));
        dunHuangShiKuTeamData.setIsDelete(rs.getBoolean("isDelete"));
        dunHuangShiKuTeamData.setCurFinishMuralId(rs.getInt("curFinishMuralId"));
        dunHuangShiKuTeamData.setMuralInfoMap(JSON.parseObject(rs.getString("muralInfoMap"), new TypeReference<Map<Integer, DunHuangShiKuMuralInfo>>() {}));

        return dunHuangShiKuTeamData;
    }

    public int getMaxTeamId(int activityId, int groupId) {
        String sql = "select ifnull(max(teamId),0) as maxId from t_c_dunhuangshiku_team_data where activityId = ? and groupId = ?";
        ResultSet rs = null;
        CallableStatement call = null;
        int maxId = 0;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++,activityId);
            call.setInt(i, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                maxId = rs.getInt("maxId");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return maxId;
    }

}
